var checkAllEles = document.querySelectorAll(".check-all");
var checkOneEles = document.querySelectorAll(".check-one");

var priceEles = document.querySelectorAll(".price");
var subEles = document.querySelectorAll(".subtotal");

var objArr = [] ;

var addEles = document.querySelectorAll(".add");
var reduceEles = document.querySelectorAll(".reduce");
var countEles = document.querySelectorAll(".count-input");

var priceTotal = document.getElementById("priceTotal");
var selectedTotal = document.getElementById("selectedTotal");

var deleteAll = document.getElementById("deleteAll");

checkOneEles.forEach(function( checkOne , key , arr ){
    /**
     * 单选联动全选
     */
    console.log(arr.length);
    var obj = {} ;
    obj.flag = false ; 
    obj.count = 1 ;
    obj.price = priceEles[key].innerHTML  ; 
    checkOne.onclick = function(){
        console.log(key);
        obj.flag = !obj.flag ; 
        console.log(objArr);
        objArr.splice(key,1,obj);
        console.log(objArr);
        priceTotal.innerHTML = `${(totalPrice(objArr)).toFixed(2)}`
        selectedTotal.innerHTML = `${totalCount(objArr)}` ; 
    }
    /**
     * 添加与价格
     */
    var counts = parseInt(countEles[key].value) ; 
    addEles[key].onclick = function (){
        countEles[key].value = parseInt(countEles[key].value) + 1; 
        counts = countEles[key].value ;
        if( counts > 1 ){
            reduceEles[key].innerHTML ="-" ; 
            reduceEles[key].disabled = false;
        } 
        obj.count = parseInt(countEles[key].value) ; 
        subEles[key].innerHTML = `${(parseInt(countEles[key].value) * priceEles[key].innerHTML).toFixed(2)}`
        console.log(objArr);
        objArr.splice(key,1,obj);
        console.log(objArr);
        priceTotal.innerHTML = `${(totalPrice(objArr)).toFixed(2)}`
        selectedTotal.innerHTML = `${totalCount(objArr)}` ; 
    }
    /**
     * 减少与价格
     */
    if( countEles[key].value == 1 ){
        reduceEles[key].innerHTML ="" ; 
    } 
    reduceEles[key].onclick = function (){
        counts = countEles[key].value ;
        if( parseInt(countEles[key].value) > 1 ){
            countEles[key].value = parseInt(countEles[key].value) - 1; 
            if( parseInt(countEles[key].value) == 1 ){
                reduceEles[key].innerHTML ="" ; 
            }
        }
        
        counts = countEles[key].value ;
        obj.count = parseInt(countEles[key].value) ; 
        subEles[key].innerHTML = `${(parseInt(countEles[key].value) * priceEles[key].innerHTML).toFixed(2)}`
        objArr.splice(key,1,obj);
        priceTotal.innerHTML = `${(totalPrice(objArr)).toFixed(2)}`;
        selectedTotal.innerHTML = `${totalCount(objArr)}` ; 
    }
    objArr.push(obj);
});


/**
 * 全选联动单选
 */
checkAllEles.forEach(function(checkAllEle,key,arr){
    checkAllEle.onclick = function (){
        if(checkAllEle.checked) {
            checkOneEles.forEach(function(checkOneEle,j){
                var obj = {} ;
                obj.flag = true ; 
                obj.count = parseInt(countEles[j].value) ;
                if ( objArr[j] == undefined ){
                    objArr.push(obj);
                } else {
                    objArr.splice(j,1,obj);
                }
                obj.price = priceEles[j].innerHTML  ; 

                checkOneEle.checked = true;
                // for( let i = 0 ; i < newArr.length ; i ++ ){
                //     if ( newArr[i] == undefined || newArr[i] == false){
                //         newArr[i] = true ; 
                //     }
                // }
            });
        } else {
            checkOneEles.forEach(function(checkOneEle,j){
                var obj = {} ;
                obj.flag = false ; 
                obj.count = parseInt(countEles[j].value) ;
                if ( objArr[j] == undefined ){
                    objArr.push(obj);
                } else {
                    objArr.splice(j,1,obj);
                }
                obj.price = priceEles[j].innerHTML  ; 
                checkOneEle.checked = false;
                // for( let i = 0 ; i < newArr.length ; i ++ ){
                //     if ( newArr[i] == undefined || newArr[i] == true ){
                //         newArr[i] = false ; 
                //     }
                //  }
            });
        }
        arr.forEach(function(item){
            item.checked = checkAllEle.checked ;
        })
        priceTotal.innerHTML = `${(totalPrice(objArr)).toFixed(2)}`;
        selectedTotal.innerHTML = `${totalCount(objArr)}` ;
    }
})


/**
 * 单删除操作
 */
 var delBtns = document.querySelectorAll(".delete");
 var trEles = document.querySelectorAll("tbody tr");
 delBtns.forEach(function(delBtn,key){
    delBtn.onclick = function (){
        trEles[key].remove();
        objArr.splice(key,1);
        priceTotal.innerHTML = `${(totalPrice(objArr)).toFixed(2)}`;
        selectedTotal.innerHTML = `${totalCount(objArr)}` ;
        console.log(objArr);
    }
 });

 /**
  * 全删操作
  */
  console.log(objArr);
  deleteAll.onclick = function (){
    let length = objArr.length ; 
    for( var key = 0 ; key < length ; key ++ ){
        trEles[key].remove();
        objArr.splice(key,1);
    }
    priceTotal.innerHTML = `${(totalPrice(objArr)).toFixed(2)}`;
    selectedTotal.innerHTML = `${totalCount(objArr)}` ;
}

 function countPrice(price,count) {
     return price * count ; 
}

 function totalPrice( arr ){
    var total = 0 ;
    arr.forEach(function ( obj , key ){
        if (obj.flag) {
            total += countPrice(obj.price,obj.count); 
        }
    });
    return total ; 
 }

 function totalCount( arr ){
     var total = 0 ; 
     arr.forEach(function ( obj ){
        if (obj.flag) {
            total += parseInt(obj.count) ;
        }
    });
    return total ;
 }